package swingxrcp.framework.client.action;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;

import swingxrcp.framework.util.log.Logger;
import swingxrcp.framework.util.log.LoggerFactory;
import swingxrcp.framework.util.serialize.JavaSerializer;
import swingxrcp.framework.util.serialize.Serializer;

/**
 * 传输器骨架实现
 *
 * @author liangfei
 * @since 2008-10-27
 */
public abstract class HttpTransporter implements Transporter {

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	protected final Serializer serializer = new JavaSerializer();

	private final static String HTTP_PROTOCAL = "http://";

	private final String prefix;

	private final String suffix;

	public HttpTransporter(String host, int port, String context, String suffix) {
		if (host == null)
			throw new java.lang.NullPointerException(
					"host == null, 服务器主机名不能为空!");
		String prefix = HTTP_PROTOCAL + host;
		if (port != 80)
			prefix += ":" + port;
		if (context != null && context.length() > 0)
			prefix += "/" + context;
		prefix += "/";
		this.prefix = prefix;
		this.suffix = (suffix == null ? "" : "." + suffix);
	}

	public Serializable transport(String actionName, Serializable object) throws IOException {
		return serializer.deserialize(doTransport(prefix + actionName + suffix, object));
	}

	protected abstract InputStream doTransport(String url, Serializable object) throws IOException;

}
